﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Elderos.AI;
using Elderos.Utils.Logging;
using Encog.ML.Data.Basic;
using Encog.Neural.Networks;

namespace AccordTrainer
{
    public class PlainQualityComputer : ThresholdQualityComputer
    {
        public override Quality ComputeQuality(TrainItem<ItemInfo>[] trainItems, double threshold)
        {
            int truePositive = 0;
            int falsePositive = 0;
            int trueNegative = 0;
            int falseNegative = 0;

            foreach (var trainItem in trainItems)
            {
                double ideal = trainItem.Output;
                double real = trainItem.AdditionalInfo.RealWeight;

                bool positive = ideal > 0;

                //if (Math.Abs(real - ideal) < threshold)
                if (real >= threshold)
                {
                    //выиграло
                    if (positive)
                    {
                        //должно было выиграть
                        truePositive++;
                    }
                    else
                    {
                        falsePositive++;
                    }
                }
                else
                {
                    //проиграло
                    if (positive)
                    {
                        //должно было выиграть
                        falseNegative++;
                    }
                    else
                    {
                        trueNegative++;
                    }
                }
            }

            Logger.Info("True positive: " + truePositive);
            Logger.Info("False positive: " + falsePositive);
            Logger.Info("True negative: " + trueNegative);
            Logger.Info("False negative: " + falseNegative);

            var quality = new Quality
                              {
                                  TruePositive = truePositive,
                                  TrueNegative = trueNegative,
                                  FalsePositive = falsePositive,
                                  FalseNegative = falseNegative,
                                  Threshold = threshold
                              };

            return quality;
        }
    }
}
